home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / AbstractUndoableEdit.java < prev    next >
Text File  |  1998-06-30  |  5KB  |  209 lines

  1. /*
  2.  * @(#)AbstractUndoableEdit.java    1.9 97/08/15
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.undo;
  22.  
  23. /**
  24.  * An abstract implementation of UndoableEdit, implementing simple
  25.  * responses to all boolean methods in that interface. 
  26.  */
  27. public class AbstractUndoableEdit implements UndoableEdit {
  28.  
  29.     /**
  30.      * String returned by getUndoPresentationName()
  31.      */
  32.     protected static final String UndoName = "Undo";    // PENDING(rjrjr): these need 
  33.  
  34.     /**
  35.      * String returned by getRedoPresentationName()
  36.      */
  37.     protected static final String RedoName = "Redo";    // to be localizable
  38.  
  39.     /**
  40.      * Defaults to true. Becomes false if this edit is undone, true
  41.      * again if it is redone.  
  42.      */
  43.     boolean hasBeenDone;
  44.  
  45.     /**
  46.      * True if this edit has not received die().
  47.      */
  48.     boolean alive;
  49.  
  50.     public AbstractUndoableEdit() {
  51.     super();
  52.  
  53.     hasBeenDone = true;
  54.     alive = true;
  55.     }
  56.  
  57.     /**
  58.      * Sets alive to false. Note that this is a one way operation:
  59.      * dead edits cannot be resurrected.  Sending undo() or redo() to
  60.      * a dead edit results in an exception being thrown.
  61.      *
  62.      * Typically an edit is killed when it is consolidated by another
  63.      * edit's addEdit() or replaceEdit() method, or when it is
  64.      * dequeued from an UndoManager
  65.      */
  66.     public void die() {
  67.     alive = false;
  68.     }
  69.  
  70.     /**
  71.      * Throws CannotUndoException if canUndo() returns false. Sets
  72.      * hasBeenDone to false. Subclasses should override to undo the
  73.      * operation represented by this edit. Override should begin with
  74.      * a call to super.
  75.      *
  76.      * @see    #canUndo
  77.      */
  78.     public void undo() throws CannotUndoException {
  79.     if (!canUndo()) {
  80.         throw new CannotUndoException();
  81.     }
  82.     hasBeenDone = false;
  83.     }
  84.  
  85.     /**
  86.      * Returns true if this edit is alive and hasBeenDone is true.
  87.      *
  88.      * @see     #die
  89.      * @see    #undo
  90.      * @see    #redo
  91.      */
  92.     public boolean canUndo() {
  93.     return alive && hasBeenDone;
  94.     }
  95.  
  96.     /**
  97.      * Throws CannotRedoException if canRedo() returns false. Sets
  98.      * hasBeenDone to true. Subclasses should override to redo the
  99.      * operation represented by this edit. Override should begin with
  100.      * a call to super.
  101.      *
  102.      * @see    #canRedo
  103.      */
  104.     public void redo() throws CannotRedoException {
  105.     if (!canRedo()) {
  106.         throw new CannotRedoException();
  107.     }
  108.     hasBeenDone = true;
  109.     }
  110.  
  111.     /**
  112.      * Returns true if this edit is alive and hasBeenDone is false.
  113.      *
  114.      * @see     #die
  115.      * @see    #undo
  116.      * @see    #redo
  117.      */
  118.     public boolean canRedo() {
  119.     return alive && !hasBeenDone;
  120.     }
  121.     
  122.     /**
  123.      * This default implementation returns false. 
  124.      *
  125.      * @see UndoableEdit#addEdit
  126.      */
  127.     public boolean addEdit(UndoableEdit anEdit) {
  128.     return false;
  129.     }
  130.  
  131.     /**
  132.      * This default implementation returns false. 
  133.      *
  134.      * @see UndoableEdit#replaceEdit
  135.      */
  136.     public boolean replaceEdit(UndoableEdit anEdit) {
  137.     return false;
  138.     }
  139.  
  140.     /**
  141.      * This default implementation returns true. 
  142.      *
  143.      * @see UndoableEdit#isSignificant
  144.      */
  145.     public boolean isSignificant() {
  146.     return true;
  147.     }
  148.  
  149.     /**
  150.      * This default implementation returns "". Used by
  151.      * getUndoPresentationName() and getRedoPresentationName() to
  152.      * construct the strings they return. Subclasses shoul override to
  153.      * return an appropriate description of the operation this edit
  154.      * represents.
  155.      *
  156.      * @see    #getUndoPresentationName
  157.      * @see    #getRedoPresentationName
  158.      */
  159.     public String getPresentationName() {
  160.     return "";
  161.     }
  162.  
  163.     /**
  164.      * If getPresentationName() returns "", returns
  165.      * AbstractUndoableEdit.UndoName. Otherwise returns
  166.      * AbstractUndoableEdit.UndoName followed by a space and
  167.      * getPresentationName()
  168.      *
  169.      * @see #getPresentationName
  170.      */
  171.     public String getUndoPresentationName() {
  172.     String name = getPresentationName();
  173.     if (name != "") {
  174.         name = UndoName + " " + name;
  175.     } else {
  176.         name = UndoName;
  177.     }
  178.  
  179.     return name;
  180.     }
  181.  
  182.     /**
  183.      * If getPresentationName() returns "", returns
  184.      * AbstractUndoableEdit.RedoName. Otherwise returns
  185.      * AbstractUndoableEdit.RedoName followed by a space and
  186.      * getPresentationName()
  187.      *
  188.      * @see #getPresentationName
  189.      */
  190.     public String getRedoPresentationName() {
  191.     String name = getPresentationName();
  192.     if (name != "") {
  193.         name = RedoName + " " + name;
  194.     } else {
  195.         name = RedoName;
  196.     }
  197.  
  198.     return name;
  199.     }
  200.  
  201.     public String toString()
  202.     {
  203.     return super.toString()
  204.         + " hasBeenDone: " + hasBeenDone
  205.         + " alive: " + alive;
  206.     }
  207. }
  208.  
  209.